require 'rbof'

class App < OF::SimpleApp
  include OF
  
  def setup
    noFill
    background 0, 0, 0
    setVerticalSync true
    
    @font = TrueTypeFont.new
    font_size = 32
    @font.loadFont "verdana.ttf", font_size
    @font.setLineHeight font_size * 1.25
  end

  def draw
    setColor 0xffffff
    drawBitmapString "click to view the bounding box", 20, 40
    
    str = millis_to_print
    @font.drawString str, 20, 100
    
    # draw the bounding box
    if @pressed
      setColor 0xff0000
      box = @font.getStringBoundingBox str, 20, 100
      rect box.x, box.y, box.width, box.height
    end
  end
  
  def millis_to_print
    result = getElapsedTimeMillis.to_s
    %w[
      zero one two three four
      five six seven eight nine
    ].each_with_index do |s, i|
      result.gsub!(/#{i}/) {"#{s}\n"}
    end
    result
  end
  
  def mousePressed(x, y, button)
    @pressed = true
  end
  
  def mouseReleased  
    @pressed = false
  end
end

OF.setupOpenGL 400, 400, OF::WINDOW
OF.runApp App.new